package com.wanxi.dangdang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.mapper.PermissionMapper;
import com.wanxi.dangdang.model.vo.PermissionFindPageVO;
import com.wanxi.dangdang.model.pojo.Permission;
import com.wanxi.dangdang.model.vo.PermissionFindByIdVo;
import com.wanxi.dangdang.model.vo.PermissionVo;
import com.wanxi.dangdang.service.PermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/9/1
 * @Version: 1.0
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> getPermissionsByUserId(Long id) {
        return permissionMapper.getPermissionsByUserId(id);
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    @Override
    public PermissionFindByIdVo findById(Long id) {
        Permission permission = permissionMapper.findById(id);
        PermissionFindByIdVo permissionFindByIdVo = new PermissionFindByIdVo();
        BeanUtils.copyProperties(permission,permissionFindByIdVo);
        return permissionFindByIdVo;
    }

    @Override
    public PageInfo<PermissionFindPageVO> findPage(PermissionVo permissionVo) {
        PageHelper.startPage(permissionVo.getPageNum(),permissionVo.getPageSize());
        List<PermissionFindPageVO> page = permissionMapper.findPage(permissionVo);
        PageInfo<PermissionFindPageVO> permissionPageInfo = new PageInfo<>(page);
        return permissionPageInfo;
    }

    @Override
    public void deleteById(Long id) {
        permissionMapper.deleteRolePermissionById(id);
        permissionMapper.deleteById(id);
    }

    @Override
    public void updateById(Permission permission) {
        permissionMapper.updateById(permission);
    }

    @Override
    public List<PermissionFindPageVO> findPermissionsByRoleId(Long id) {
        return permissionMapper.findPermissionsByRoleId(id);
    }

    @Override
    public String checkName(String name) {
        return permissionMapper.checkName(name);
    }
}
